//
//  StressSevenRecordsCell.swift
//  DaRings
//
//  Created by 尹琼 on 2022/11/3.
//

import UIKit
import MYModule

class StressSevenRecordsCell: TableViewCell {
    
    var stressModels: [SingleStressModel]? {
        didSet {
            
            guard let stressModels = stressModels else {
                return
            }

            bind(stressModels: stressModels)
        }
    }
    
    let sevenTimeChartView = SevenTimeBarChartView()
    
    override func makeUI() {
        super.makeUI()
        
        contentView.backgroundColor =  UIColor.darkBackground
        sevenTimeChartView.cornerRadius = 18.0
        
        sevenTimeChartView.backgroundColor = UIColor.background
        sevenTimeChartView.titleLabel.font = Fonts.systemFont(ofSize: 14)
        sevenTimeChartView.titleLabel.textColor = UIColor.assist_222222
       
        self.sevenTimeChartView.barChartView.setConfig(self.config())
      
        
        contentView.addSubview(sevenTimeChartView)
        
        sevenTimeChartView.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.trailing.leading.equalToSuperview()
            make.bottom.equalToSuperview().offset(-20)
        }
        
    }
    
    
    func config(isEmpty: Bool = true) -> DFBarChartViewConfig {
        
        let xAxisLabelColor = UIColor.Stress.theme
        let xAxisLabelTextColor = isEmpty == false ? UIColor.Stress.theme : UIColor.assist_FFFFFF
        
        let config = DFBarChartViewConfig.init(xAxisLabelFont: Fonts.systemFont(ofSize: 10),
                                               xAxisLabelTextColor: xAxisLabelTextColor,
                                               xAxisLineColor: xAxisLabelColor)
        return config
    }
    
   
    
    func valueFormatter(of data: [Double]) -> DataCompareAxisValueFormatter {
        let xAxisValues = data.map { i ->String in
            
            if i == 0 {
                return ""
            }
            
            return "\(Int(i))"
        }
        
      return DataCompareAxisValueFormatter(axisValueArray: xAxisValues)
    }
    
    func bind(stressModels: [SingleStressModel]) {
        
        let barHightLightColor = UIColor.Step.theme
        let barNormalColor = UIColor.Step.theme
        
        let barValues: [SevenTimeBarChartView.BarValue] =  stressModels.map {
            
            SevenTimeBarChartView.BarValue(time: $0.end, data: Double($0.stress))
        }
        
        let config = self.config(isEmpty: false)
        let datas = barValues.map { $0.data }
        let xValueFormatter = self.valueFormatter(of: datas)
        let barModel = SevenTimeBarChartView.BarsModel(color: barNormalColor, hightLightColor: barHightLightColor, bars: barValues, xAxisValueFormatter: xValueFormatter, xConfig: config, YMaxValue: 100)
        
        sevenTimeChartView.barModel = barModel
        
    }

}
